var fr = require('hy5b/backend/read');
var path = require('path');
var iconv = require('iconv-lite');

var split = require("hy5b/t").split;

var resourceProperty = require('../common/config.js');

var util = require('../common/util.js');
var url = require('url');

const myExports = {
  '/weixin': function(req, res, next, filter, serverOptions) {
    if (0) {
      util.setHeader(res, 'Cache-Control', 'private, max-age=0, no-cache');
      util.setHeader(res, 'Pragma', 'no-cache');
      // 以上两个也没有什么卵用, 但是微信端需要有才有效
      util.setHeader(res, 'Expires', 'Mon, 26 Jul 1970 05:00:00 GMT'); //并没有什么卵用,没有的话微信端会加载很慢
    }
    
    if (1) {
      var pathname = req.url;
      if (pathname === '/weixin' || pathname === '/weixin/' || pathname === '/weixin/index.html') {
        res.redirect('/weixin/index.html?openId=' + resourceProperty.getOpenId());
      } else if (req.query.openId && req.query.openId != resourceProperty.getOpenId()) {
        res.redirect(req.url.replace('openId=' + req.query.openId, 'openId=' + resourceProperty.getOpenId()));
      } else {
        if (1) {
          next();
        } else {
          util.proxy(req, res, resourceProperty.getUrl());
        }
      }
    } else {
      next();
      // util.proxy(req, res, resourceProperty.getUrl());
    }
  },
  '/weixin/js/app/index.js': function(req, res, next, filter, serverOptions) {
    fr.readFile(path.resolve(serverOptions.root, 'weixin/js/app/index.js'), function(err, line) {
      var js ='';

      split(line, /\/\/ *nodejs\-debug\-start/, /\/\/ *nodejs\-debug\-end/, function(a,b) {
        js += a;
        js += b?"function getLocation() {$.ajax({url: '/wx/signature',dataType: 'json',success: onSuccess})}":'';
      })
      res.end(js);
    });
  },
  '/followgiant': function(req, res) {
    util.proxy(req, res);
  },
  '/node_modules': function(req, res) {
    util.proxy(req, res);
  },
  '/theme': function(req, res) {
    util.proxy(req, res);
  },
  '/icons': function(req, res) {
    util.proxy(req, res);
  },
  '/tenant': function(req, res, next) {
    if (resourceProperty.USE_BASE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/portal': function(req, res, next) {
    if (resourceProperty.USE_BASE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/home': function(req, res, next) {
    util.proxy(req, res, resourceProperty.getRootUrl(), null, req.url.replace('/home', ''));
  },
  '/static': function(req, res, next) {
    if (resourceProperty.USE_BASE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/uyun-base': function(req, res, next) {
    if (resourceProperty.USE_BASE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/udap-solution': function(req, res, next) {
    if (resourceProperty.USE_BASE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
      // util.proxy(req, res, 'http://10.1.240.56:8940', null, req.url.replace('/udap-solution', ''));
      // util.proxy(req, res, 'http://10.1.240.39:8940', null, req.url.replace('/udap-solution', ''));
    }
  },
  '/dragon': function(req, res, next) {
    util.proxy(req, res, resourceProperty.getRootUrl());
  },
  '/omp': function(req, res, next) {
    util.proxy(req, res, resourceProperty.getRootUrl());
  },
  '/videomon': function(req, res) {
    util.proxy(req, res, resourceProperty.getRootUrl());
  },
  '/cmdb': function(req, res, next) {
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/monitor/': function(req, res) {
    util.proxy(req, res, resourceProperty.getRootUrl());
  },
  '/chatops': function(req, res) {
    util.proxy(req, res, resourceProperty.getRootUrl());
  },
  '/dashboard/api': function(req, res, next) {
    if (resourceProperty.USE_BASE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/dashboard/': function(req, res) {
    util.proxy(req, res, 'http://192.168.76.5:5000', null, req.url.replace('/dashboard', ''));
    // util.proxy(req, res, resourceProperty.getRootUrl());
  },
  '/dashboard': function(req, res) {
    res.writeHead(301, { 'Location': '/dashboard/' });
    res.end()
  },
  '/databank/': function(req, res, next) {
    // util.proxy(req, res, 'http://192.168.76.5:3000', null, req.url.replace('/databank', ''));
    util.proxy(req, res, resourceProperty.getRootUrl());
  },
  '/databank': function(req, res) {
    res.writeHead(301, { 'Location': '/databank/' });
    res.end()
  },
  '/report': function(req, res, next) {
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/sockjs-node': function(req, res, next) {
    util.proxy(req, res, 'http://192.168.76.5:5000');
  },
  '/notify': function(req, res, next) {
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/collector': function(req, res, next) {
    // util.proxy不能代理静态的图片, 现在可以了
    //只是可以请求，但是不能上传
    //上传已完成，可以放行至/api通用代理
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/frontend': function(req, res, next) {
    if (resourceProperty.USE_BASE_MOCK) {
      req.url = req.url.replace(/\?.*/, '');
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/upload/': function(req, res, next) {
    // util.proxy不能代理静态的图片, 现在可以了
    //只是可以请求，但是不能上传
    //上传已完成，可以放行至/api通用代理
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getUrl(), null, req.url.replace(/^\/show/, ''));
    }
  },
  '/file': function(req, res, next) {
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getUrl(), null, req.url.replace(/^\/show/, ''));
    }
  },
  '/resource': function(req, res, next) {
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getUrl(), null, req.url.replace(/^\/show/, ''));
    }
  },
  '/extensions': function(req, res, next) {
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getUrl(), null, req.url.replace(/^\/show/, ''));
    }
  },
  '/show/upload/': function(req, res, next) {
    myExports['/upload/'](req, res, next);
  },
  '/show/file': function(req, res, next) {
    myExports['/file'](req, res, next);
  },
  '/show/resource': function(req, res, next) {
    myExports['/resource'](req, res, next);
  },
  '/showdata': function(req, res, next) {
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  '/show/api': function(req, res, next) {
    if (resourceProperty.USE_MOCK) {
      next();
    } else {
      util.proxy(req, res, resourceProperty.getRootUrl());
    }
  },
  // '/show': function(req, res, next) {
  //   util.proxy(req, res, 'http://192.168.76.5:8000', null, req.url.replace('/show', ''));
  // }
  '/rwt/124': function(req, res, next) {
    if (/\.aspx/.test(req.url)) {
      util.proxy(req, res, 'http://61.175.198.136:8083', function(res, body) {
        // console.log('------------', res.get('Set-Cookie'))
        let bodyString = iconv.decode(body, "GB2312").replace(/http:\/\/61\.175\.198\.136:8083\//g, "http://" + req.headers.host + '/');
        res.end(iconv.encode(bodyString, "GB2312"));
        return true;
      });
    } else {
      util.proxy(req, res, 'http://61.175.198.136:8083');
    }
  },
  '/Original/BornMag.aspx': function(req, res, next) {
    const ourl = url.parse(req.url);
    util.setHeader(res, 'Location', "http://" + req.headers.host + "/rwt/124/http/QJWGP8DHMMYHM4LRF3ZXT45BNZYGG5C" + ourl.path);
    res.statusCode = 302;
    res.end();
  },
  '/App_Themes': function(req, res, next) {
    util.proxy(req, res, 'http://61.175.198.136:8083/rwt/124/http/QJWGP8DHMMYHM4LRF3ZXT45BNZYGG5C');
  },
  '/reader3': function(req, res, next) {
    util.proxy(req, res, 'http://61.175.198.136:8083/rwt/124/http/QJWGP8DHMMYHM4LRF3ZXT45BNZYGG5C');
  },
};

module.exports = myExports;
